package com.zk.shop.service.Impl;

import com.zk.shop.dao.OrderDao;
import com.zk.shop.entity.Order;
import com.zk.shop.service.OrderService;
import com.zk.shop.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    private SqlSession session= null;
    private OrderDao orderDao= null;

    @Override
    public List<Order> showOrderAndUser() {
        List<Order> orderList = null;
        try{
            session = SqlSessionFactoryUtil.getSqlSession();
            orderDao = session.getMapper(OrderDao.class);
            orderList =  orderDao.showOrderAndUser();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return  orderList;
    }

    @Override
    public boolean addOrder(List<Order> orderList) {
        boolean flag = false;
        try{
            session = SqlSessionFactoryUtil.getSqlSession();
            orderDao = session.getMapper(OrderDao.class);
            flag = orderDao.addOrder(orderList);
            session.commit();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }
    @Override
    public boolean updateOrder(Order order) {
        boolean flag = false;
        try{
            session = SqlSessionFactoryUtil.getSqlSession();
            orderDao = session.getMapper(OrderDao.class);
            flag = orderDao.updateOrder(order);
            System.out.println(flag);
            session.commit();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }

    @Override
    public boolean delOrder(Order order) {
        boolean flag = false;
        try{
            session = SqlSessionFactoryUtil.getSqlSession();
            orderDao = session.getMapper(OrderDao.class);
            flag = orderDao.delOrder(order);
            System.out.println(111);
            System.out.println(flag);
            session.commit();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }
    @Override
    public List<Order> selOrder(Order order) {
        List<Order> orderList = null;
        try{
            session = SqlSessionFactoryUtil.getSqlSession();
            orderDao = session.getMapper(OrderDao.class);
            orderList =  orderDao.selOrder(order);

            /*for (int i = 0; i < orderList.size(); i++) {

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = formatter.format(orderList.get(i).getOrderTime());
                Date data=formatter.parse(dateString);
                orderList.get(i).setOrderTime(data);
                System.out.println(orderList.get(i).getOrderTime());
            }*/

        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return  orderList;
    }



    @Override
    public Order selOneOrder(int orderId) {
        Order order=null;
        try{
            session = SqlSessionFactoryUtil.getSqlSession();
            orderDao = session.getMapper(OrderDao.class);
            order = orderDao.selOneOrder(orderId);
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return order;
    }

    @Override
    public Order selOneOrderNum(String orderNum) {
        Order order=null;
        try{
            session = SqlSessionFactoryUtil.getSqlSession();
            orderDao = session.getMapper(OrderDao.class);
            order = orderDao.selOneOrderNum(orderNum);
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return order;
    }

    @Override
    public List<Order> selBuyerOrder(Order order) {
        List<Order> orderList = null;
        try{
            session = SqlSessionFactoryUtil.getSqlSession();
            orderDao = session.getMapper(OrderDao.class);
            orderList =  orderDao.selBuyerOrder(order);
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return  orderList;
    }

    @Override
    public List<Order> selSellerOrder(Order order) {
        List<Order> orderList = null;
        try{
            session = SqlSessionFactoryUtil.getSqlSession();
            orderDao = session.getMapper(OrderDao.class);
            orderList =  orderDao.selSellerOrder(order);
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return  orderList;
    }

}
